//
//  STOrderViewController.swift
//  SeaTransportManage
//
//  Created by guopengwen on 2021/11/26.
//

import UIKit

class STOrderNewViewController: GPBaseViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, STOrderNewActionDelegate  {

    let requestApi = GPRequestApi.init()
    var dataSource: Array<Dictionary<String, Any>> = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setNavBarTitle("新订单")
        addSubviewForPage()
        
        self.showLoading("正在加载...")
        requestWaitOrderList()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        requestWaitOrderList()
    }

    func addSubviewForPage() {
        addTopBGView()
        
        view.addSubview(nonDataView)
        nonDataView.snp.makeConstraints { (make) in
            make.top.equalTo(0)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(454)
        }
        
        view.addSubview(listView)
        listView.snp.makeConstraints { (make) in
            make.top.equalTo(10)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.bottom.equalTo(0)
        }
        
        nonDataView.isHidden = true
        STOrderNewCollectionViewCell.registerTo(listView)
    }
    
    func requestWaitOrderList() {
        requestApi.getCurrentWaitOrder() {
            (isSuccess, response, error)  in
            self.hideLoading()
            if(isSuccess) {
                let responseData = response as! Dictionary<String, Any>
                self.dataSource = responseData["data"] as! Array<Dictionary<String, Any>>
                self.listView.reloadData()
                self.nonDataView.isHidden = self.dataSource.count == 0
            }
        }
    }
    
    // UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataSource.count
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let item = dataSource[indexPath.item]
        let status = item["status"] as! Int
        let h = status == 2 ? (256 * (kScreenWidth - 32) / 343) : (200 * (kScreenWidth - 32) / 343)
        return CGSize.init(width: kScreenWidth - 32, height: h)
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = STOrderNewCollectionViewCell.reusableCell(collectionView, indexPath: indexPath)
        cell?.delegate = self
        let objc = dataSource[indexPath.item]
        cell?.updateCellData(data: objc,indexPath)
        return cell ?? collectionView.dequeueReusableCell(withReuseIdentifier: STOrderNewCollectionViewCell.identifier, for: indexPath)
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let item = dataSource[indexPath.item]
        let status = item["status"] as! Int
        let orderId = item["orderId"] as! Int
        let orderType = item["orderType"] as! String
        if( status != 2 ){
            let vc: STOrderNewDetailViewController = STOrderNewDetailViewController.init()
            vc.orderId = orderId
            vc.orderType = orderType
            vc.status = status
            navigationController?.pushViewController(vc, animated: true)
        }
    }
    
    // STOrderNewActionDelegate
    func handleReturnAction(indexPath: IndexPath) {
        // 退还
        let item = dataSource[indexPath.item]
        let orderId = item["orderId"] as! Int
        self.showLoading("正在加载...")
        requestApi.refusedOrder(["orderId": orderId]){
            (isSuccess, response, error) in
            self.hideLoading()
            if(isSuccess) {
                self.toastInfo("退单成功")
                self.requestWaitOrderList()
            } else {
                self.toastError(error?.msg ?? "")
            }
        }
    }
   
    func handlWaitAction(indexPath: IndexPath) {
        // 确认接单
        let item = dataSource[indexPath.item]
        let orderId = item["orderId"] as! Int
        requestApi.acceptOrder(["orderId": orderId]) {
            (isSuccess, response, error) in
            if (isSuccess) {
                self.toastInfo("接单成功")
                let vc: STOrderActionSuccessViewController = STOrderActionSuccessViewController.init()
                vc.status = item["status"] as! Int
                vc.orderType = item["orderType"] as! String
                self.navigationController?.pushViewController(vc, animated: true)
            } else {
                self.toastError(error!.1)
            }
        }
    }
    
    // 懒加载
    lazy var listView: UICollectionView = {
        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout.init()
        layout.minimumLineSpacing = 16
        layout.minimumInteritemSpacing = 0
        let collectView: UICollectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: layout)
        collectView.delegate = self
        collectView.dataSource = self
        collectView.backgroundColor = UIColor.white
        collectView.layer.cornerRadius = 8
        collectView.backgroundColor = UIColor.init(hexString: "#F7F7F7", alpha: 1)
        return collectView
    }()
    
    lazy var nonDataView: STOrderNonDataView = {
        let v = STOrderNonDataView.init(frame: CGRect.zero)
        return v
    }()
}
